<?php

/**
 * This is the model class for table "tbl_user_logs".
 *
 * The followings are the available columns in table 'tbl_user_logs':
 * @property integer $id
 * @property integer $user_id
 * @property string $datetime
 * @property string $action_table
 * @property integer $action_id_table
 * @property string $ip_user
 * @property integer $type
 *
 * The followings are the available model relations:
 * @property Users $user
 */
class UserLogs extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return UserLogs the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'tbl_user_logs';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('action_table, action_id_bid, type, action_id_create', 'required'),
            array('user_id, action_id_bid, action_id_create, type', 'numerical', 'integerOnly'=>true),
            array('action_table', 'length', 'max'=>30),
            array('ip_user', 'length', 'max'=>50),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, user_id, datetime, action_table, action_id_bid, action_id_create, ip_user, type', 'safe', 'on'=>'search'),
        );
    }

    static function saveLogsUser($action_table,$action_id_create,$action_id_bid, $type)
    {

        //action_table la ten cua bảng dữ liệu
        //action_id_table la id của bảng đó
        //type là hành động cập nhật: create là 1,update là hai,xóa là 3
        $model = new UserLogs;
        $model->user_id = Yii::app()->user->id;
        $model->action_id_create= $action_id_create;
        $model->action_table = $action_table;
        $model->action_id_bid = $action_id_bid;
        $model->ip_user= $_SERVER['REMOTE_ADDR'];
        $model->type=$type;
        $model->save(false);
       // Yii::app()->end();

    }
    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'user' => array(self::BELONGS_TO, 'Users', 'user_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'user_id' => 'User',
            'datetime' => 'Datetime',
            'action_table' => 'Action Table',
            'action_id_bid' => 'Action Id Bid',
            'action_id_create' => 'Action Id Create',
            'ip_user' => 'Ip User',
            'type' => 'Type',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('user_id',$this->user_id);
        $criteria->compare('datetime',$this->datetime,true);
        $criteria->compare('action_table',$this->action_table,true);
        $criteria->compare('action_id_bid',$this->action_id_bid);
        $criteria->compare('action_id_create',$this->action_id_create);
        $criteria->compare('ip_user',$this->ip_user,true);
        $criteria->compare('type',$this->type);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }
    
    public function search_user()
    {
        $criteria=new CDbCriteria;
        $criteria->order="id DESC";
        $criteria->condition='user_id='.Yii::app()->user->id;
        $criteria->compare('datetime',$this->datetime,true);
        $criteria->compare('action_table',$this->action_table);
        $criteria->compare('action_id_bid',$this->action_id_bid);
        $criteria->compare('action_id_create',$this->action_id_create);
        $criteria->compare('type',$this->type);

       return new CActiveDataProvider($this, array(
    			'criteria'=>$criteria,
                'pagination'=>array('pageSize'=>5),
    		));
    }
}